package com.zheng.oss.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.base.BaseController;
import com.zheng.oss.common.constant.OssResult;
import com.zheng.oss.common.constant.OssResultConstant;
import com.zheng.oss.common.constant.enums.FilePermission;
import com.zheng.oss.dao.model.OssFile;
import com.zheng.oss.dao.model.OssFileStack;
import com.zheng.oss.dao.model.OssRoleFile;
import com.zheng.oss.dto.FileInfo;
import com.zheng.oss.rpc.api.OssFileService;
import com.zheng.oss.rpc.api.OssFileStackService;
import com.zheng.oss.rpc.api.OssRoleFileService;
import com.zheng.oss.web.util.FastDFSFile;
import com.zheng.oss.web.util.FileManager;
import com.zheng.oss.web.util.UploadResult;
import com.zheng.upms.dao.model.UpmsRole;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by likun on 2018/4/25.
 */
@Controller
@RequestMapping("/fastdfs")
public class FastDFSController extends BaseController {
    @Autowired
    private OssFileStackService fileStackService;
    @Autowired
    private OssFileService fileService;
    @Autowired
    private OssRoleFileService roleFileService;
    @Autowired
    private UpmsApiService apiService;
    private final int systemRootId = 1;
    private final int personalRootId = 2;

    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public Object fileUpload(@RequestParam(value = "file", required = true) MultipartFile file) {
        UploadResult upload = FileManager.upload(file);
        return new OssResult(OssResultConstant.SUCCESS, upload);
    }


    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable String ids,
                         @RequestParam(required = false, defaultValue = "false", value = "isSystem") Boolean isSystem) {
        try {
            fileService.deleteFile(ids, getUsername(), isSystem);
        } catch (Exception e) {
            return new OssResult(OssResultConstant.FAILED, e.getMessage());
        }
        return new OssResult(OssResultConstant.SUCCESS, "删除成功");
    }
}
